/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database;

/**
 *
 * @author Tri
 */

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import java.net.UnknownHostException;



public class DBManager {
    private static DBManager instance = null;
    private Mongo m_mongo;
    private String m_host;
    private int m_port;
    private DB m_db;
    private DBManager() {
        try {
            this.m_host = DBConfig.host;
            this.m_port = DBConfig.port;
            m_mongo = new Mongo(this.m_host, this.m_port);
            m_db = m_mongo.getDB(DBConfig.dbName);
        } catch (UnknownHostException exp) {
            System.err.println(exp.getMessage());
        }
    }
    
    public static DBManager getInstance() {
        if(instance == null) {
            instance = new DBManager();
        }
        return instance;
    }
    
    public static void destroyInstance() {
        instance = null;
    }
    
    public DB getDB() {
//        m_db = m_mongo.getDB(dbName);
        
        return m_db;
    }
    
    public DBCollection getCollection(String collectionName) {
        return m_db.getCollection(collectionName);
    }
    
    public void testMethod() {
        BasicDBObject newDocument = new BasicDBObject();
        newDocument.append("$set", new BasicDBObject().append("clients", 110));
        
        newDocument.put("number", 23);
        DBCursor cursor = getCollection("hello").find(newDocument);
        while(cursor.hasNext()) {
            
        }
    }
}
